package com.bocai.kelalacar.ui.car.main.act;


import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.TravelDetailBean;
import com.bocai.kelalacar.ui.car.main.contract.TravelDetailContract;
import com.bocai.kelalacar.ui.car.main.module.TravelDetailModule;
import com.bocai.kelalacar.ui.car.main.presenter.TravelDetailPresenter;
import com.bocai.kelalacar.ui.car.order.act.MyCarOrderActivity;
import com.bocai.kelalacar.util.DoubleUtil;
import com.bocai.kelalacar.util.SP;
import com.bocai.kelalacar.util.ToastUitl;
import com.bocai.kelalacar.widget.AlertDialog;
import com.bocai.kelalacar.widget.SingleAlertDialog;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by yuanfei on 2017/6/19.
 */

public class TravelDetailActivity extends BaseActivity implements TravelDetailContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_start_position)
    TextView tvStartPosition;
    @BindView(R.id.tv_end_position)
    TextView tvEndPosition;
    @BindView(R.id.tv_user_time)
    TextView tvUserTime;
    @BindView(R.id.tv_car_num)
    TextView tvCarNum;
    @BindView(R.id.tv_car_data)
    TextView tvCarData;
    @BindView(R.id.tv_car_name)
    TextView tvCarName;
    @BindView(R.id.tv_car_phone)
    TextView tvCarPhone;
    @BindView(R.id.main)
    LinearLayout main;
    @BindView(R.id.btn_rush_order)
    Button btnRushOrder;
    @Inject
    TravelDetailPresenter presenter;
    String orderId;
    @BindView(R.id.ll_back)
    LinearLayout llBack;

    @Override
    public int getLayoutId() {
        return R.layout.act_travel_detail;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new TravelDetailModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("订单详情");
        initBack(imgBack,llBack);
        orderId = getIntent().getStringExtra("orderId");
        presenter.getTravelInfo(orderId);
        btnRushOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.grabOrder(orderId);
            }
        });
    }

    @Override
    public void getTravelInfoSuccess(TravelDetailBean bean) {
        tvStartPosition.setText(bean.getContent().getOriginCity());
        tvEndPosition.setText(bean.getContent().getArriveCity());
        tvCarNum.setText(bean.getContent().getApplySeats()+"座");
        tvCarData.setText(bean.getContent().getBeginTime());
        tvMoney.setText(DoubleUtil.m2(bean.getContent().getPrice())+"");
        String bb = bean.getContent().getContactPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        tvCarPhone.setText(bb);
        String aa = bean.getContent().getContactName().substring(0,1);
        tvCarName.setText(aa+"**");
    }

    @Override
    public void gradOrderSuccess() {
        setDialog();
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {

        if (msg.equals("您的登录已失效，请重新登录")){
            showShortToast(msg);
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }else if (msg.equals("还未通过认证")){
            if ((int) SP.get(this,"ApprovalStatus",0) == 0){
                ToastUitl.showShort("认证未申请");
            } else if ((int)SP.get(this,"ApprovalStatus",0) == -1){
                ToastUitl.showShort("审核未通过");
            }else if ((int)SP.get(this,"ApprovalStatus",0) == 1){
                ToastUitl.showShort("审核中");
            }
            finish();
        }else if (msg.equals("订单状态错误")){
            showShortToast("不好意思，订单已被抢走");
            finish();
        }else {
            showShortToast(msg);
        }
    }
    public void setDialog(){
        new SingleAlertDialog(mContext).builder().setTitle("抢单成功！请等待旅行社付款")
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(MyCarOrderActivity.class);
                        finish();
                    }
                }).show();
    }
}
